package com.ums.scumspay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ums.scumspay.common.Resp;
import com.ums.scumspay.converter.PayOrderConverter;
import com.ums.scumspay.entity.PayOrder;
import com.ums.scumspay.entity.resp.PayOrderPosResp;
import com.ums.scumspay.entity.resp.PayOrderReDataResp;
import com.ums.scumspay.service.PayOrderService;
import com.ums.scumspay.mapper.PayOrderMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import java.awt.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* @author pengdeng
* @description 针对表【pay_order】的数据库操作Service实现
* @createDate 2023-09-04 14:43:18
*/
@Service
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder>
    implements PayOrderService{

    @Autowired
    private PayOrderMapper payOrderMapper;

    @Autowired
    private PayOrderConverter payOrderConverter;

    @Value("${file.location}")
    String fileLocation;

    @Override
    public Resp<PayOrderPosResp> getPosPayInfo(String transDate) {
        String format=transDate;
        if (StringUtils.isEmpty(transDate)){
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH,-1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            format = simpleDateFormat.format(calendar.getTime());
        }
        LambdaQueryWrapper<PayOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PayOrder::getTransDate,format);
        queryWrapper.eq(PayOrder::getPayMethod,"0");
        queryWrapper.eq(PayOrder::getPayStatus,"00");
        queryWrapper.eq(PayOrder::getRefundSign,"0");
        List<PayOrder> list = payOrderMapper.selectList(queryWrapper);
        PayOrderPosResp resp = new PayOrderPosResp(payOrderConverter.entity2Resp(list), list.size());
        return Resp.success(resp);
    }

    @Override
    public Resp<PayOrderPosResp> getQrCodePayInfo(String transDate) {
        String format=transDate;
        if (StringUtils.isEmpty(transDate)){
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH,-1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            format = simpleDateFormat.format(calendar.getTime());
        }
        LambdaQueryWrapper<PayOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PayOrder::getTransDate,format);
        queryWrapper.eq(PayOrder::getPayMethod,"1");
        queryWrapper.eq(PayOrder::getPayStatus,"00");
        queryWrapper.eq(PayOrder::getRefundSign,"0");
        List<PayOrder> list = payOrderMapper.selectList(queryWrapper);
        PayOrderPosResp resp = new PayOrderPosResp(payOrderConverter.entity2Resp(list), list.size());
        return Resp.success(resp);
    }

    @Override
    public Resp<PayOrderReDataResp> reconciliation(String transDate) {
        String format=transDate;
        if (StringUtils.isEmpty(transDate)){
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH,-1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            format = simpleDateFormat.format(calendar.getTime());
        }
        // LambdaQueryWrapper<PayOrder> receiveQueryWrapper = new LambdaQueryWrapper<>();
        // receiveQueryWrapper.eq(PayOrder::getTransDate,format);
        // receiveQueryWrapper.eq(PayOrder::getRefundSign,"0");
        // List<PayOrder> receiveList = payOrderMapper.selectList(receiveQueryWrapper);
        // LambdaQueryWrapper<PayOrder> refundQueryWrapper = new LambdaQueryWrapper<>();
        // refundQueryWrapper.eq(PayOrder::getTransDate,format);
        // refundQueryWrapper.eq(PayOrder::getRefundSign,"1");
        // List<PayOrder> refundList = payOrderMapper.selectList(refundQueryWrapper);
        //
        //
        // ByteArrayOutputStream byteReceiveByteArrayOutputStream = new ByteArrayOutputStream();
        // ByteArrayOutputStream byteRefundByteArrayOutputStream = new ByteArrayOutputStream();
        // try {
        //     ObjectOutputStream receiveOutputStream = new ObjectOutputStream(byteReceiveByteArrayOutputStream);
        //     receiveOutputStream.writeObject(receiveList);
        //     byte[] receiveByteArray = byteReceiveByteArrayOutputStream.toByteArray();
        //     ObjectOutputStream refundOutputStream = new ObjectOutputStream(byteRefundByteArrayOutputStream);
        //     refundOutputStream.writeObject(refundList);
        //     byte[] refundByteArray = byteRefundByteArrayOutputStream.toByteArray();
        //     String replace = format.replace("-", "");
        //     PayOrderReDataResp resp = new PayOrderReDataResp();
        //     resp.setReceiveByteArray(receiveByteArray);
        //     resp.setRefundByteArray(refundByteArray);
        //     resp.setReceiveFileName("re-"+replace+".txt");
        //     resp.setRefundFileName("rf-"+replace+".txt");
        //     byteReceiveByteArrayOutputStream.close();
        //     byteRefundByteArrayOutputStream.close();
        //     receiveOutputStream.close();
        //     refundOutputStream.close();
        //     return Resp.success(resp);
        // } catch (IOException e) {
        //     throw new RuntimeException(e);
        // }
        format=format.replace("-","");
        String filePath ="D:\\code\\file"+"\\"+format;
        File file = new File(filePath);
        File[] files = file.listFiles();
        byte[] receiveByteArray = null;
        byte[] refundByteArray = null;
        PayOrderReDataResp resp = new PayOrderReDataResp();
        if (files!=null){
            for (File file1 : files) {
                if (file1.getName().contains("re")){
                    receiveByteArray=getByte(file1);
                }else {
                    refundByteArray=getByte(file1);
                }
            }
            resp.setReceiveByteArray(receiveByteArray);
            resp.setRefundByteArray(refundByteArray);
            resp.setReceiveFileName("re-"+format+".txt");
            resp.setRefundFileName("rf-"+format+".txt");
        }

        return Resp.success(resp);

    }

    @Override
    public void createFile() {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(calendar.getTime());

        LambdaQueryWrapper<PayOrder> receiveQueryWrapper = new LambdaQueryWrapper<>();
        receiveQueryWrapper.eq(PayOrder::getTransDate,format);
        receiveQueryWrapper.eq(PayOrder::getPayStatus,"00");
        receiveQueryWrapper.eq(PayOrder::getRefundSign,"0");
        List<PayOrder> receiveList = payOrderMapper.selectList(receiveQueryWrapper);
        downloadFile(receiveList,format,"re");

        LambdaQueryWrapper<PayOrder> refundQueryWrapper = new LambdaQueryWrapper<>();
        refundQueryWrapper.eq(PayOrder::getTransDate,format);
        refundQueryWrapper.eq(PayOrder::getPayStatus,"02");
        refundQueryWrapper.eq(PayOrder::getRefundSign,"1");
        List<PayOrder> refundList = payOrderMapper.selectList(refundQueryWrapper);
        downloadFile(refundList,format,"rf");
    }


    public void downloadFile(List<PayOrder> list,String date,String flag) {
        date=date.replace("-","");
        String filePath =fileLocation+"\\"+date;
        File file = new File(filePath);
        if (!file.exists()){
            file.mkdirs();
        }
        filePath=filePath+"\\"+flag+"-"+date+".txt";
        try {
            FileWriter fileWriter = new FileWriter(new File(filePath));
            for (int i = 0; i <list.size(); i++) {
                fileWriter.write(list.get(i).toString());
                if (i!=list.size()-1){
                    fileWriter.write(",");
                }
            }
            fileWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public byte[] getByte(File file){

        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
       return buffer;
    }

    public static void main(String[] args) throws IOException {

    }

}




